package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.clinic.DiseaseData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/3/12 17:02
 */
public class ClinicDiseaseDaoImpl extends TempDataObjectDao<DiseaseData> {
    public static String addSql = "insert into t_u_clinic_disease(userId, id, level, value) values(?,?,?,?)";
    @Override
    protected boolean addData(DiseaseData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        if (getDb().executeNoneQuery(addSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update t_u_clinic_disease set level = ?, value=? where userId=? and id=?";
    @Override
    protected boolean updateData(DiseaseData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DiseaseData parseData(ResultSet rs) throws SQLException {
        DiseaseData data = new DiseaseData();
        data.setLevel(rs.getInt("level"));
        data.setValue(rs.getLong("value"));
        data.setUserId(rs.getLong("userId"));
        data.setId(rs.getInt("id"));


        return data;
    }

    public Map<Integer, DiseaseData> getDiseaseDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DiseaseData> diseaseDataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_u_clinic_disease where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                DiseaseData diseaseData = parse(rs);
                diseaseDataMap.put(diseaseData.getId(), diseaseData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return diseaseDataMap;
    }
}
